import { keepQuotationApi } from "@/api/configCenter/warehouseEntryChannel";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import dayjs from "dayjs";

export default function OperationHook(state: Base.GeneraDataType) {
  const { drawerOpts, formState, dialogOpts, emit, passData, loading, getEditBaseRateTime } = state;
  const { resetFields } = FormHook({ formState });
  const submitHandle = () => {
    const params = {
      channelId: passData.value.id,
      ...formState,
      beginTime: getEditBaseRateTime.value ? dayjs(formState.beginTime).format("YYYY-MM-DD HH:mm:ss") : dayjs().format("YYYY-MM-DD HH:mm:ss"), //未点击编辑里的生效时间时，使用当前时间
      itemList: formState.itemList.map((t: any) => {
        return {
          ...t,
          countryList: t.countryList.map((r: any) => {
            return {
              countryId: r
            };
          })
        };
      })
    };
    loading.value = true;
    keepQuotationApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };
  const cancelHandle = () => {
    resetFields();
    emit("update:visible", false);
  };
  const editRateHandle = () => {
    drawerOpts.title = "设置运价";
    drawerOpts.component = "EditBaseRate";
    drawerOpts.passData = formState;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.visible = true;
  };
  const addCustomerLevelHandle = () => {
    formState.customerLevelList.push({
      customerLevelId: undefined,
      adjustmentValue: undefined,
      unit: undefined
    });
  };
  const deleteCustomerLevelHandle = (index: number) => {
    formState.customerLevelList.splice(index, 1);
  };
  const changeUnitHandle = (row: WarehouseEntryChannel.CustomerLevel) => {
    if (Number(row.unit) === 1) row.adjustmentValue = 1;
    else row.adjustmentValue = undefined as unknown as number;
  };
  // 新增客户调价
  const addCustomerAdjustmentHandle = () => {
    dialogOpts.title = "新建客户调价";
    dialogOpts.width = "520px";
    dialogOpts.component = "AddCustomerAdjust";
    dialogOpts.type = 1;
    dialogOpts.visible = true;
  };
  // 编辑
  const editCustomerAdjustmentHandle = (row: Base.GeneraDataType) => {
    dialogOpts.title = "编辑客户调价";
    dialogOpts.width = "520px";
    dialogOpts.component = "AddCustomerAdjust";
    dialogOpts.type = 2;
    dialogOpts.passData = row;
    dialogOpts.visible = true;
  };
  // 新增附加费
  const addChargeHandle = () => {
    dialogOpts.title = "新增附加费用";
    dialogOpts.width = "520px";
    dialogOpts.component = "AddCharge";
    dialogOpts.type = 1;
    dialogOpts.visible = true;
  };
  // 编辑附加费
  const editChargeHandle = (row: Base.GeneraDataType) => {
    dialogOpts.title = "新增附加费用";
    dialogOpts.width = "520px";
    dialogOpts.component = "AddCharge";
    dialogOpts.passData = row;
    dialogOpts.type = 2;
    dialogOpts.visible = true;
  };
  //历史报价
  const historyHandle = (row: Base.GeneraDataType) => {
    drawerOpts.title = "历史报价";
    drawerOpts.width = "1200px";
    drawerOpts.component = "ShowHistory";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  // 删除附加费
  const deleteChargeHandle = (rowIndex: number) => {
    formState.additionalChargesList.splice(rowIndex, 1);
  };
  // 删除
  const deleteCustomerAdjustmentHandle = (index: number) => {
    formState.customerAdjustmentList.splice(index, 1);
  };
  const dialogSubmitHandle = (state?: any) => {
    dialogOpts.visible = false;
    if (dialogOpts.component === "AddCustomerAdjust") {
      if (dialogOpts.type === 1) {
        formState.customerAdjustmentList.push(state);
      } else {
        Object.assign(dialogOpts.passData, state);
      }
    } else if (dialogOpts.component === "AddCharge") {
      if (dialogOpts.type === 1) {
        formState.additionalChargesList.push(state);
      } else {
        Object.assign(dialogOpts.passData, state);
      }
    }
  };

  //设置运价抽屉回调控制
  const editBaseSubmitHandle = (e: boolean) => {
    drawerOpts.visible = false;
    getEditBaseRateTime.value = e;
  };
  return {
    editRateHandle,
    deleteCustomerLevelHandle,
    addCustomerLevelHandle,
    changeUnitHandle,
    addCustomerAdjustmentHandle,
    editCustomerAdjustmentHandle,
    deleteCustomerAdjustmentHandle,
    dialogSubmitHandle,
    addChargeHandle,
    editChargeHandle,
    deleteChargeHandle,
    cancelHandle,
    submitHandle,
    resetFields,
    historyHandle,
    editBaseSubmitHandle
  };
}
